package org.itcast.adi.planinfo.controller;

import org.itcast.adi.planinfo.dao.PlanInfoMapper;
import org.itcast.adi.planinfo.entity.PageData;
import org.itcast.adi.planinfo.entity.PlanInfo;
import org.itcast.adi.planinfo.entity.ResultBean;
import org.itcast.adi.planinfo.service.PlanInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/planinfo")
public class PlanInfoController {
    @Autowired
    private PlanInfoService planInfoService;

    @Autowired
    private PlanInfoMapper planInfoMapper;

    @PostMapping("/save")
    public ResultBean save(@RequestBody PlanInfo planInfo) {
        planInfoService.save(planInfo);
        return ResultBean.success();
    }

    // 分页查询
    @PostMapping("/findByPage/{pageNum}/{pageSize}")
    public ResultBean save(@PathVariable("pageNum") int pageNum,
                           @PathVariable("pageSize") int pageSize,
                           @RequestParam("pageNum") int num,
                           @RequestParam("pageSize") int size,
                           @RequestBody PlanInfo planInfo
                           ) {
        System.out.println(pageNum+":"+pageSize+":"+num+":"+size);
        PageData pageData = planInfoService.findByPage(pageNum,pageSize,planInfo);
        return ResultBean.success(pageData);
    }

    @PostMapping("/findAll")
    public ResultBean findAll(@RequestBody PlanInfo planInfo
    ) {
        List<PlanInfo> planInfos = planInfoMapper.findAll(planInfo);
        return ResultBean.success(planInfos);
    }

    // 查询单个
    @GetMapping("/findById")
    public ResultBean findById(@RequestHeader("Connection") String connection,
            @RequestParam("planId") String planId
    ) {
        System.out.println(connection);
        PlanInfo planInfo = planInfoMapper.findById(planId);
        return ResultBean.success(planInfo);
    }

    @GetMapping("/findById/{parentId}")
    public ResultBean findById1(
            @PathVariable("parentId") String parentId,
            @RequestParam("planDesc") String planDesc
    ) {
        List<PlanInfo> planInfos = planInfoMapper.findByDesc(planDesc,parentId);
        return ResultBean.success(planInfos);
    }

    @PostMapping("/findAllOne/{planId}")
    public ResultBean findAllOne(
            @PathVariable("planId") String planId,
            @RequestBody PlanInfo planInfo
    ) {
        System.out.println(planId);
        List<PlanInfo> planInfos = planInfoMapper.findAll(planInfo);
        return ResultBean.success(planInfos);
    }
}
